package com.alan.camunda.boot.delegate;

import com.alan.camunda.boot.dto.MimeString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * 默认情况下，代理类中使用的 MyBatisPlus Mapper 与 Camunda 引擎使用同一个事务
 */
@Slf4j
@Component("cv")
public class ChangeVariablesDelegate implements JavaDelegate {


    private final RuntimeService runtimeService;

    public ChangeVariablesDelegate(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    @Override
    public void execute(DelegateExecution execution) throws Exception {
//        Map<String, Object> variables = execution.getVariables();
//        JSONObject userdto = (JSONObject)variables.get("userdto");
//        if (userdto != null) {
//
//        }
//        String parentExecutionId = execution.getParentId();
////        Execution parentExecution = runtimeService.createExecutionQuery().executionId(parentExecutionId).singleResult();
//        execution.setVariable("isLocal",  55);
//
//
//        execution.setVariableLocal("_T",  55);
//        execution.setVariableLocal("_R",  55);
//
//        execution.setVariable("notLocal",  55);


        ClassPathResource res = new ClassPathResource("gbk_file.txt");
        try {
            for (String s : IOUtils.readLines(res.getInputStream(), Charset.forName("GBK"))) {
                System.out.println(s);

                MimeString mimeString = new MimeString();
                mimeString.setContent(s);

                execution.setVariable("mimeString", mimeString);

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }




//        log.info("环节: {} , 代理类可访问变量为: \n{}", execution.getCurrentActivityName(), JSON.toJSONString(variables, true));

    }


}
